package com.zhentao.controller;

import com.zhentao.pojo.TbMessage;
import com.zhentao.service.TbMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 教师端消息控制器（与前端一致，路径为/message/...）
 */
@RestController
@RequestMapping("/message")
public class TbMessageController {

    @Autowired
    private TbMessageService tbMessageService;

    /**
     * 获取接收者的消息列表
     * receiverType: 1学生 2教师
     */
    @GetMapping("/list")
    public List<TbMessage> list(@RequestParam("receiverId") Long receiverId,
                                @RequestParam("receiverType") Integer receiverType) {
        return tbMessageService.listByReceiver(receiverId, receiverType);
    }

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public String send(@RequestBody TbMessage message) {
        boolean ok = tbMessageService.sendMessage(message);
        return ok ? "发送成功" : "发送失败";
    }

    /**
     * 标记已读
     */
    @PostMapping("/read")
    public String markRead(@RequestParam("id") Long id) {
        boolean ok = tbMessageService.markRead(id);
        return ok ? "已标记为已读" : "标记失败";
    }
}